<?php
namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Order\Order;
use App\Models\User\BankCard;
use App\Models\User\Record;
use App\Models\User\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;


class RecordController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {

            $Record = $this->filters($request, new Record());

            if($request->get("user_id")) {
                $Record = $Record->where("user_id", $request->get("user_id"));
            }

            if($request->get("type")) {
                $Record = $Record->where("type", $request->get("type"));
            }

            $paginator = $Record->orderBy($request->get("sidx"), $request->get("sord"))->paginate($request->get("rows"));

            $rows = [];
            foreach ($paginator as $page) {
                $rows[] = $page;
            }

            if ($rows) {
                $ids = [];
                foreach ($rows as $p) {
                    $ids[$p->user_id] = $p->user_id;
                }
                sort($ids);
                $users = User::whereIn("id", $ids)->get();
            }

            foreach ($rows as $p) {
                foreach ($users as $p2) {
                    if ($p->user_id == $p2->id) {
                        $p->nickname = $p2->nickname;
                    }
                }
            }

            $data = [
                "rows" => $rows,
                "total"=> $paginator->lastPage(),
                "page" => $paginator->currentPage(),
                "records" => $paginator->count(),
            ];

            return response()->json($data);

        } else {

            return view("user/record");
        }
    }
}
